package com.juzi.polymorphic;

/**
 * 目标：学习多态形势下的类中转换机制
 */
public class Test2 {
    public static void main(String[] args) {
        //自动类型转换
        Animal a = new Dog();
        a.run();


        //强制类型转换
        Animal b = new Dog();
        b.run();
//        Tortoise t = (Tortoise) b;//从父类类型转到子类类型，必须强制类型转换
//        t.layEggs();
//        ((Tortoise) b).layEggs();

//        Dog d = (Dog) b;//强制类型转换，编译阶段不报错，（注意：有继承或者实现关系，
        // 编译阶段可以强制，没有毛病），运行时可能出错

        if (b instanceof Tortoise) {
            Tortoise t = (Tortoise) b;
            t.layEggs();
        }else if (b instanceof Dog){
            Dog d = new Dog();
            d.lookDoor();
        }


        System.out.println("------------------------------------");
        go(new Dog());
        go(new Tortoise());
    }


    public static void go(Animal a){
        a.run();
        //a到底是王八还是狗
        if (a instanceof Tortoise) {
            Tortoise t = (Tortoise) a;
            t.layEggs();
        }else if (a instanceof Dog){
            Dog d = new Dog();
            d.lookDoor();
        }
    }
}
